home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #12 / Amiga Plus CD - 2002 - No. 12.iso / AmigaOS / Aplus_Dev / AP-Website / download / pgp / pgp5gui-174b.lha / PGP5GUI-Src.lha / PGP5GUI-Src / SelPGPKeyMisc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-06-23  |  8.9 KB  |  303 lines

  1. /*
  2. ** PGP5GUI - A GUI using Magic User Interface v3.8
  3. **
  4. ** Copyright 23-JUNE-1998 by Stefan Zakarias, All Rights Reserved.
  5. **
  6. ** This source code is released as FREEWARE - Use it for whatever you like,
  7. ** as long as NO financial reward is gained by you for such usage.
  8. **
  9. ** If you use any parts of the this source code for anything, give ME credit
  10. ** wherever credit is due, please ;-)
  11. */
  12.  
  13. /*
  14. ** Generate key-info strings for a MUI ListView...
  15. **
  16. ** From a text output file we previously created using "PGPK -l >PGP5Keys":
  17. **
  18. **
  19. ** Type Bits KeyID    Created    Expires    Algorithm       Use
  20. ** pub  1024 E06CBF11 1997-04-18 ---------- RSA             Sign and Encrypt
  21. ** uid  Shane Cracknell <shane@amitar.com.au>
  22. **
  23. ** pub  1024 04A76854 1997-10-24 ---------- DSS             Sign and Encrypt
  24. ** sub  2048 64B2CFEC 1997-10-24 ---------- Diffie-Hellman
  25. ** uid  Shanel Cracknell <Shane@amitar.com.au>
  26. **
  27. ** sec+ 1024 CA214F18 1997-10-18 ---------- DSS             Sign and Encrypt
  28. ** sub  2048 D6F69DA2 1997-10-18 ---------- Diffie-Hellman
  29. ** uid  Stefan L. Zakarias <stef@amitar.com.au>
  30. **
  31. ** sec  1024 E6042DD9 1997-08-24 ---------- RSA             Sign and Encrypt
  32. ** uid  Stefan Zakarias <stef@amitar.com.au>
  33. **
  34. ** 4 matching keys found
  35. **
  36. ** >>> OR WITH UNIX PGP5.0i - "Final"... <<<
  37. **
  38. ** Type Bits KeyID      Created    Expires    Algorithm       Use
  39. ** pub  1024 0xE06CBF11 1997-04-18 ---------- RSA             Sign and Encrypt
  40. ** uid  Shane Cracknell <shane@amitar.com.au>
  41. **
  42. ** pub  1024 0x04A76854 1997-10-24 ---------- DSS             Sign and Encrypt
  43. ** sub  2048 0x64B2CFEC 1997-10-24 ---------- Diffie-Hellman
  44. ** uid  Shanel Cracknell <Shane@amitar.com.au>
  45. **
  46. ** sec+ 1024 0xCA214F18 1997-10-18 ---------- DSS             Sign and Encrypt
  47. ** sub  2048 0xD6F69DA2 1997-10-18 ---------- Diffie-Hellman
  48. ** uid  Stefan L. Zakarias <stef@amitar.com.au>
  49. **
  50. ** sec  1024 0xE6042DD9 1997-08-24 ---------- RSA             Sign and Encrypt
  51. ** uid  Stefan Zakarias <stef@amitar.com.au>
  52. **
  53. ** 4 matching keys found
  54. **
  55. **
  56. ** We'll extract a line with the format of "Algo KeyID   Created    UserID"
  57. **
  58. ** We then write lines, like the following, to the temporary file "T:selected".
  59. **
  60. ** RSA E06CBF11 1997-04-18 Shane Cracknell <shane@amitar.com.au>
  61. ** DSS 04A76854 1997-10-24 Shanel Cracknell <Shane@amitar.com.au>
  62. ** DSS CA214F18 1997-10-18 Stefan L. Zakarias <stef@amitar.com.au>
  63. ** RSA E6042DD9 1997-08-24 Stefan Zakarias <stef@amitar.com.au>
  64. */
  65.  
  66. #include <exec/types.h>
  67. #include <exec/memory.h>
  68. #include <dos/dos.h>
  69. #include <utility/tagitem.h>
  70. #include <dos/exall.h>
  71.  
  72. #include <string.h>
  73.  
  74. #include <proto/exec.h>
  75. #include <proto/dos.h>
  76. #include <clib/alib_stdio_protos.h>
  77.  
  78. /* Include GUI stuff */
  79. #include "SelPGPKeyGUI.h"
  80.  
  81. /* Parser modes */
  82. #define PUB_KEY 1
  83. #define GET_UID    2
  84.  
  85. /* The key-info table (1024 keys max.) */
  86. char *keystable[1025];
  87.  
  88. /*
  89. ** Generates the strings for the listview.
  90. ** Returns: 0 = No error, -1 = filename not found.
  91. */
  92. int
  93. GenerateKeyStrings(char *filename)
  94. {
  95.     char *buffer;
  96.     char *indptr;
  97.  
  98.     char algobuff[ALGO_SIZE];        /* Buffer for algorithm type */
  99.     char keyIDbuff[KEYID_SIZE];        /* Buffer for key ID */
  100.     char createdbuff[CREATED_SIZE];    /* Buffer for created date */
  101.     char userIDbuff[USERID_SIZE];    /* Buffer for user ID. */
  102.  
  103.     char parsedline[BUFFER_SIZE];    /* Buffer for parsed line */
  104.     char linebuff[USERID_SIZE];        /* Buffer for incoming line */
  105.  
  106.     BPTR filelock, filehandle;
  107.  
  108.     int i, len, keynum = 0, parsemode = PUB_KEY;
  109.  
  110.     /* Quick-check to see if the file exists! */
  111.     filelock = Lock(filename, ACCESS_READ);
  112.  
  113.     /* Did we get a lock on that file? */
  114.     if (filelock)
  115.     {
  116.         /* File was found...  We can (have to?) unlock it now */
  117.         UnLock(filelock);
  118.  
  119.         /*
  120.         ** We have a record of possibly key-info..
  121.         ** Open key-info file so we can access it
  122.         */
  123.         filehandle = Open(filename, MODE_OLDFILE);
  124.  
  125.         /* Did DOS give us the O.K. to read it? */
  126.         if (filehandle)
  127.         {
  128.             /*
  129.             ** DOS says it's OK to use!
  130.             ** Read a string of upto 255 bytes from the record into 'linebuff'.
  131.             ** 'buffer' is a pointer to 'linebuff' or NULL if EOF/ERROR.
  132.             ** We break out of loop on NULL/EOF/ERROR.
  133.             ** You need to use IOErr() to see if EOF or ERROR.
  134.             */
  135.  
  136.             /* Free up any previously initialised 'keystable[]' memory */
  137.             for (i = 0; i < 1024; i++)
  138.             {
  139.                 if (keystable[i])
  140.                 {
  141.                     FreeVec(keystable[i]);
  142.                     keystable[i] = 0;
  143.                 }
  144.             }
  145.  
  146.             buffer = linebuff;
  147.  
  148.             /* While the buffer has something... */
  149.             while (buffer)
  150.             {
  151.                 /* Get next line */
  152.                 buffer = FGets(filehandle, linebuff, (USERID_SIZE - 1));
  153.  
  154.                 /* Get length of line */
  155.                 len = strlen(buffer);
  156.  
  157.                 /* If not empty line and have less than 1025 entries... */
  158.                 if ((len > 3) && (keynum < 1024))
  159.                 {
  160.                     /* Get info from the lines according to parse-mode */
  161.                     switch (parsemode)
  162.                     {
  163.                     case PUB_KEY:
  164.                     /*
  165.                     ** We check for 'pub' or 'sec' in first 3 chars. of a line.
  166.                     ** When either of these is found, we do some processing and
  167.                     ** go on to GET_UID.
  168.                     */
  169.                         /*
  170.                         ** Put a terminating NULL at 4th. char. of buffer...
  171.                         ** We only have to check the 1st. 3 chars. of the line next!
  172.                         */
  173.  
  174.                         *(buffer + 3) = NULL;
  175.  
  176.                         /* Get a copy of buffer address */
  177.                         indptr = buffer;
  178.  
  179.                         /* Check first 3 chars. for 'pub' or 'sec' */
  180.                         if ((strncmp(indptr, "pub", 3) == 0) ||
  181.                             (strncmp(indptr, "sec", 3) == 0))
  182.                         {
  183.                             /* Point to beginning of Key Size (Bits) */
  184.                             indptr += 5;
  185.                             *(indptr + 4) = NULL;    /* Null terminate string */
  186.  
  187.                             /* Point to beginning of KeyID */
  188.                             indptr += 5;
  189.  
  190.                             if (*(indptr + 1) == 'x')
  191.                             {
  192.                                 /* KeyID starts with '0x'... */
  193.                                 *(indptr + (KEYID_SIZE - 1)) = NULL;    /* Null terminate string */
  194.  
  195.                                 /* Get key's ID Number ('0x' prefixed) */
  196.                                 strcpy(keyIDbuff, indptr);
  197.  
  198.                                 /* Point to the beginning of 'Created' date */
  199.                                 indptr += KEYID_SIZE;
  200.                             }
  201.                             else
  202.                             {
  203.                                 /* KeyID does NOT start with '0x'... */
  204.                                 *(indptr + (KEYID_SIZE - 3 )) = NULL;    /* Null terminate string */
  205.  
  206.                                 /* Get key's ID Number (not '0x' prefixed) */
  207.                                 sprintf(keyIDbuff, "0x%s", indptr);
  208.  
  209.                                 /* Point to the beginning of 'Created' date */
  210.                                 indptr += (KEYID_SIZE - 2);
  211.                             }
  212.  
  213.                             *(indptr + (CREATED_SIZE - 1)) = NULL;    /* Null terminate string */
  214.  
  215.                             /* Get key's created date */
  216.                             strcpy(createdbuff, indptr);
  217.  
  218.                             /* Point to beginning of 'Expires' date */
  219.                             indptr += 11;
  220.                             *(indptr + 10) = NULL;    /* Null terminate string */
  221.  
  222.                             indptr += 11;    /* Point to beginning of 'Algo' */
  223.                             *(indptr + (ALGO_SIZE - 1)) = NULL;    /* Null terminate string */
  224.  
  225.                             /* Get key's algorithm type */
  226.                             strcpy(algobuff, indptr);
  227.  
  228.                             /* Next mode... */
  229.                             parsemode = GET_UID;
  230.                         }
  231.  
  232.                         break;
  233.  
  234.                     case GET_UID:
  235.                     /*
  236.                     ** We've just moved to a new line...
  237.                     ** We check for 'uid' in first 3 chars. of a line.
  238.                     ** When this is found, we store the rest of the line.
  239.                     ** Then we go back to parsing for PUB_KEY.
  240.                     */
  241.                         /*
  242.                         ** Put a terminating NULL at 4th. char. of buffer...
  243.                         ** We check only first 3 chars. of the line next.
  244.                         */
  245.  
  246.                         *(buffer + 3) = NULL;
  247.                         indptr = buffer;    /* Get a copy of buffer address */
  248.  
  249.                         /* Check first 3 chars. for 'uid' */
  250.                         if ((strncmp(indptr, "uid", 3) == 0))
  251.                         {
  252.                             /* Point to beginning of user ID */
  253.                             indptr += 5;
  254.  
  255.                             len = strlen(indptr);
  256.                             *(indptr + (len - 1)) = NULL;
  257.  
  258.                             /* Get key's user ID */
  259.                             strcpy(userIDbuff, indptr);
  260.  
  261.                             /* Build up final line */
  262.                             sprintf(parsedline,"%s %s %s %s",
  263.                                     algobuff,keyIDbuff,createdbuff,userIDbuff);
  264.  
  265.                             /* Get length of built line */
  266.                             len = strlen(parsedline);
  267.  
  268.                             /* Does line have something? */
  269.                             if (len)
  270.                             {
  271.  
  272.                                 /* Allocate memory and store pointer for line */
  273.                                 keystable[keynum] = AllocVec(len+1, MEMF_PUBLIC|MEMF_CLEAR);
  274.  
  275.                                 /* Copy built line to allocated memory */
  276.                                 strcpy(keystable[keynum], parsedline);
  277.  
  278.                                 /* Increment array pointer */
  279.                                 keynum++;
  280.                             }
  281.  
  282.                             /* Next mode... */
  283.                             parsemode = PUB_KEY;
  284.                         }
  285.  
  286.                         break;
  287.                     }
  288.                 }
  289.             }
  290.  
  291.             /* Finished with the file... */
  292.             Close(filehandle);
  293.  
  294.             /* Store a NULL to show end of entries */
  295.             keystable[keynum] = 0;
  296.  
  297.             return (0);        /* Return - No Error */
  298.         }
  299.     }
  300.  
  301.     return (-1);        /* Return - Error! */
  302. }
  303.